<template>
  <div class="topbar">
    <div class="wrap topbarWrap">
      <div class="l">欢迎来到叩丁狼积分商城</div>
      <ul class="r">
        <li>
          <img
            class="avatar"
            :src="userInfo.headImg"
            width="26"
            alt=""
          />
          用户名:{{userInfo.nikName}}
        </li>
        <li>我的积分:{{userInfo.coin}}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" @click="changeIsLoginShow(true)" v-else>登录</li>
      </ul>
    </div>
  </div>
</template>
                     
<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { weiXinLoginAPI,UserInfoAPI } from "@/request/api";

export default {
  name: "LaoliuHeader",

  data() {
    return {
    
    };
  },

  mounted() {},

  methods: {
    ...mapMutations({
      // 方法名："模块名/方法名"
      changeIsLoginShow: "showModal/changeIsLoginShow",
      loginStatus: "logiinStatus/loginStatus",
      changeUserInfo:"userInfo/changeUserInfo",
      initUserInfo:"userInfo/initUserInfo"

    }),
    ...mapActions({
      asyncChangeIsShowToast: "showToast/asyncChangeIsShowToast",
      asyncChangeUserInfo:'userInfo/asyncChangeUserInfo'
      
    }),
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.logiinStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,

    }),
  },
  async created() {
    // console.log(this.$route.query.code);
    
    console.log("进来了topbar-created的钩子");
    let code = this.$route.query.code;
    if (code) {
      let res = await weiXinLoginAPI({
        code,
      });
      console.log("微信登录后res", res);

      if (res.data.code == 0) {
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "登录成功",
          type: "warning",
        });
        // 村token
        localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
        // 更新购物车的显示
        this.loginStatus(true);
        this.$route.query.code && this.$router.push(this.$route.path);
        this.asyncChangeUserInfo()
      } else if (res.data.code == 400) {
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "二维码已失效，请重新打开",
          type: "warning",
        });
        this.changeIsLoginShow(true);
      } else if (res.data.code == 407) {
        this.asyncChangeIsShowToast({
          isshow: true,
          msg: "请使用手机号再进行登录",
          type: "warning",
        });
        this.changeIsLoginShow(true);
        localStorage.setItem("uuid", res.data.uuid);
      }
    }else{
      let token = localStorage.getItem("x-auth-token");
      this.loginStatus(Boolean(token))
      if(token){
        let res = await UserInfoAPI()
        if(res.data.code == 0){
          this.changeUserInfo(res.data.data)
        }
      }else{
        this.initUserInfo()
      }
    }
  },
  // watch: {
  //   // 监听路由地址的变化

  //   "$route.path": {
  //     handler(newVal,oldVal) {
  //       console.log("wath..",newVal,oldVal );
  //       if(newVal != oldVal) {
  //         let token = localStorage.getItem("x-auth-token");
  //         this.loginStatus(Boolean(token))
  //         console.log("执行了",this.loginStatus(Boolean(token)));
  //       }
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  .topbarWrap {
    color: #fffefe;
    display: flex;
    justify-content: space-between;
    .r {
      li {
        float: left;
        margin-left: 20px;
        text-align: center;
        cursor: pointer;
        &.login-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
      .avatar {
        vertical-align: middle;
        border-radius: 50%;
      }
    }
  }
}
</style>